#include "FILTERS_test.h"
#include "../functions.h"
#include <time.h>
#include <helper_image.h>
#include <sys/time.h>
#include <math.h>
#define _USE_MATH_DEFINES


using namespace std;
using std::cout;
using std::endl;

/**
 * compare the time between cpu and gpu algorithms
 * @param img
 * @param width integer that represents the width of the image
 * @param height integer that represents the height of the image
 * @param dims
 * @param pixelsPerThread integer that represents how match pixels a thread has to work with
 */
void compararFiltro(uchar4* img, int width, int height, int* dims, int pixelsPerThread,int op){
	int kx = 3;
	int ky=3;
	bool b = false;
	struct timeval tiGPU, tfGPU,tiCPU, tfCPU;
	double tiempoGPU,tiempoCPU;
	uchar4* gpu = NULL;
	uchar4* cpu = NULL;
	switch(op){
	case 1 :
		gettimeofday(&tiGPU, NULL);
		gpu = media_gpu(img, width, height,dims,kx,ky,pixelsPerThread);
		gettimeofday(&tfGPU, NULL);

		gettimeofday(&tiCPU, NULL);
		cpu=media_cpu(img,width,height,kx,ky);
		gettimeofday(&tfCPU, NULL);
		break;
	case 2 :
		gettimeofday(&tiGPU, NULL);
		gpu = maximo_gpu(img, width, height,dims,kx,ky,pixelsPerThread);
		gettimeofday(&tfGPU, NULL);

		gettimeofday(&tiCPU, NULL);
		cpu=maximo_cpu(img,width,height,kx,ky);
		gettimeofday(&tfCPU, NULL);
		break;

	default :
		gettimeofday(&tiGPU, NULL);
		gpu = minimo_gpu(img, width, height,dims,kx,ky,pixelsPerThread);
		gettimeofday(&tfGPU, NULL);

		gettimeofday(&tiCPU, NULL);
		cpu=minimo_cpu(img,width,height,kx,ky);
		gettimeofday(&tfCPU, NULL);
		break;
	}
	tiempoGPU= (tfGPU.tv_sec - tiGPU.tv_sec)*1000 + (tfGPU.tv_usec - tiGPU.tv_usec)/1000.0;
		cout << "\n * GPU --"
             << "Ha tardado: "<<tiempoGPU<< " milisegundos\n"<< endl;

	tiempoCPU= (tfCPU.tv_sec - tiCPU.tv_sec)*1000 + (tfCPU.tv_usec - tiCPU.tv_usec)/1000.0;
	cout << " * CPU --"
					<< "Ha tardado: "<<tiempoCPU<< " milisegundos\n"<< endl;

	cout <<  " la GPU fue " << tiempoCPU/tiempoGPU << " veces mas rapida" << endl;
	b = compareUchar4(gpu,cpu,width,height);
	if(!b)
		printf("false\n");
	else
		printf("true\n");
	sdkSavePPM4ub("cpu.ppm",reinterpret_cast<unsigned char*>(cpu),width,height);
	sdkSavePPM4ub("gpu.ppm",reinterpret_cast<unsigned char*>(gpu),width,height);
	//free(gpu);

}

